package com.malie.myspringboot.rabbitmq.springbootRabbitmq;

import com.malie.myspringboot.common.UserPlusEntity;
import com.malie.myspringboot.mybatisPlus.MyBatisPlusDemoMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceiveHandler {

    private static int sum = 0;
    @Autowired
    MyBatisPlusDemoMapper myBatisPlusDemoMapper;

    //监听队列1
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue1", durable = "false"),
            exchange = @Exchange(
                    value = "fanout.huang",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            ),
            key = {}))
    public void rece_1(String msg) {
        int i = 1;
        System.out.println(" [老黄fanout] received : " + msg + "!");
        sum++;
        if (sum == 10) {
            //写表
            UserPlusEntity userPlusEntity = new UserPlusEntity();
            userPlusEntity.setName("第" + i + "条数据");
            i++;
            myBatisPlusDemoMapper.insert(userPlusEntity);
            sum = 0;
        }

    }

    // 监听队列2
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue2", durable = "false"),
            exchange = @Exchange(
                    value = "fanout.huang",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.FANOUT
            ),
            key = {}))
    public void recv2(String msg) {
        System.out.println(" [老黄fanout2] received : " + msg + "!");
    }

   /* //监听短信队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue_sms", durable = "false"),
            exchange = @Exchange(
                    value = "topic.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"topic.#.sms.#"}))
    public void rece_sms(String msg) {
        System.out.println(" [短信服务] received : " + msg + "!");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue_huang", durable = "false"),
            exchange = @Exchange(
                    value = "topic.huang",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"topic.#"}))
    public void rece_sms11(String msg) {
        System.out.println(" [老黄] received : " + msg + "!");
    }*/
}
